package com.factory.code.common.bean;

import cn.hutool.core.io.FileUtil;
import com.factory.code.common.helper.FilePathHelper;
import com.factory.code.service.ProjectService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.net.InetAddress;

@Component
public class ApplicationRunnerImpl implements ApplicationRunner {
    private static final Logger logger = LoggerFactory.getLogger(ApplicationRunnerImpl.class);


    @Value("${server.port:8080}")
    private String port;

    @Autowired
    private ProjectService projectService;


    @Override
    public void run(ApplicationArguments args) {
        try {
            String downloadPath = FilePathHelper.getDownloadPath();
            InetAddress addr = InetAddress.getLocalHost();

            logger.info("[CodeFactory] 已启动 请访问: http://{}:{}", addr.getHostAddress(), port);
            //清空文件下载
            delDownloadPath(downloadPath);
            //初始化数据
            initializeData();


        } catch (Exception e) {
            logger.error("[历史数据清空] 异常:{}", e.getMessage());
        }

    }

    private void initializeData() {
        projectService.initializeData();

    }

    /**
     * 清空文件下载
     */
    private void delDownloadPath(String downloadPath) {
        logger.info("[历史数据清空] 清空文件下载目录开始");
        //可以用  但会把download删除
        FileUtil.del(downloadPath);
        logger.info("[历史数据清空] 目录已清空   目录: {}", downloadPath);
    }

}
